package com.rock.code.report.inspect.controller;

import com.rock.code.base.controller.BaseController;
import com.rock.code.common.comm.DateUtil;
import com.rock.code.common.comm.DateUtil2;
import com.rock.code.common.comm.QuartzUtil;
import com.rock.code.report.inspect.service.JbInspectViewService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
* 巡检
*
* @author xubh
* @date 2018/01/21
*/
@Controller
@RequestMapping("/InspectReport")
public class JbInspectViewController extends BaseController {

    /**
    * The JbInspectViewService
    */
    @Resource
    private JbInspectViewService jbInspectViewService;

    @RequestMapping("")
    public String inspectPage(){
        return "inspect/inspectView";
    }
    /**
     * 巡检视图中的任务分组
     * @return
     */
    @RequestMapping(value = "/taskGroupPollingOverview" , method = RequestMethod.POST)
    @ResponseBody
    public HashMap taskGroupPollingOverviewController(){

        HashMap hashMap =jbInspectViewService.taskGroupPollingOverviewService();
        return hashMap;
    }

    /**
     * queryInspectTask 巡检任务
     *
     * @return TbSysDepartment
     * @throws Exception the exception
     * @author xubh
     * @date 2018/01/06
     */
    @ResponseBody
    @RequestMapping(value = "/queryInspectTask", method = RequestMethod.POST)
    public HashMap queryInspectTask() throws Exception {
        HashMap resultMap=new HashMap();
        List resultList=new ArrayList();
        try{
            List list1=jbInspectViewService.queryInspectAllTask();//查询巡检任务总数
            HashMap map1=(HashMap)list1.get(0);
            resultList.add(map1);

            List list2=jbInspectViewService.queryAutoManualInspectTask();//查询自动和手动巡检任务数
            HashMap map2_auto=(HashMap)list2.get(0);
            map2_auto.put("DESCRIBE","自动巡检任务数");
            HashMap map2_manual=(HashMap)list2.get(1);
            map2_manual.put("DESCRIBE","手动巡检任务数");
            resultList.add(map2_auto);
            resultList.add(map2_manual);

            List list3=jbInspectViewService.queryTaskExecuteStatus();//查询任务执行成功/失败/执行中任务数
            HashMap map3_success=new HashMap();
            HashMap map3_fail=new HashMap();
            HashMap map3_execute=new HashMap();
            String successCount="0";//执行成功任务数
            String failCount="0";//执行失败任务数
            String executeCount="0";//执行中任务数
            if(list3!=null&&list3.size()>0){
                for(int i=0;i<list3.size();i++){
                    HashMap map3=(HashMap)list3.get(i);
                    String status=map3.get("STATUS").toString();
                    if("5".equals(status)){
                        successCount=map3.get("COUNT").toString();
                    }else if("2".equals(status)){
                        failCount=map3.get("COUNT").toString();
                    }else {
                        executeCount=map3.get("COUNT").toString();
                    }
                }
            }
            map3_success.put("COUNT",successCount);
            map3_success.put("DESCRIBE","任务成功数");
            map3_fail.put("COUNT",failCount);
            map3_fail.put("DESCRIBE","任务失败数");
            map3_execute.put("COUNT",executeCount);
            map3_execute.put("DESCRIBE","执行中任务数");
            resultList.add(map3_success);
            resultList.add(map3_fail);
            resultList.add(map3_execute);

            List var8 =jbInspectViewService.queryPlanExecuteCount();//计划执行数
            int var3 = 0;//计划执行任务数
            QuartzUtil var4 = new QuartzUtil();
            SimpleDateFormat var5 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            HashMap var6 = new HashMap();
            Date var7 = new Date();
            int var19;
            if (var8 != null && var8.size() > 0) {
                for(Iterator var9 = var8.iterator(); var9.hasNext(); var3 += var19 > 0 ? var19 : 0) {
                    Map var10 = (Map)var9.next();
                    String var11 = var10.get("TASK_ID").toString();
                    String var12 = var10.get("CRONTAB").toString();
                    Date var13 = (Date)var10.get("CREATE_TIME");
                    Object var14 = var10.get("PREV_FIRE_TIME");
                    String var15 = var10.get("ENABLE").toString();
                    Date var16 = (Date)var10.get("UPDATE_TIME");
                    Long var17 = -1L;
                    if (var14 != null) {
                        var17 = Long.parseLong(var10.get("PREV_FIRE_TIME").toString());
                    }

                    new Date();
                    Date var18;
                    if (var17 == -1L) {
                        var18 = var13;
                    } else {
                        var18 = new Date(var17);
                    }

                    var19 = 0;
                    String var20 = "";
                    if (DateUtil.getDate(var7).equals(DateUtil.getDate(var13))) {
                        String var21 = DateUtil.getDateTime("yyyy-MM-dd", var13);
                        var20 = var21 + " 23:59:59";
                        if ("2".equals(var15)) {
                            if (DateUtil2.after(var5.parse(var20), var16)) {
                                var20 = var5.format(var16);
                            } else {
                                var20 = var21 + " 23:59:59";
                            }
                        }

                        var19 = var4.getExeTimes(var12, var13, var5.parse(var20), var18).size();
                    } else if (DateUtil2.after(var7, var13)) {
                        String var22 = DateUtil.getDateTime("yyyy-MM-dd", var7);
                        String var23 = var22 + " 00:00:00";
                        var20 = var22 + " 23:59:59";
                        if ("2".equals(var15)) {
                            if (DateUtil2.after(var5.parse(var20), var16)) {
                                var20 = var5.format(var16);
                            } else {
                                var20 = var22 + " 23:59:59";
                            }
                        }

                        String[] var24 = var12.split(" ");
                        if (var24[3].contains("/")) {
                            List var25 = var4.getExeTimes(var12, var5.parse(var23), var5.parse(var20), var18);
                            Iterator var26 = var25.iterator();

                            while(var26.hasNext()) {
                                Date var27 = (Date)var26.next();
                                if (var22.equals(DateUtil.getDateTime("yyyy-MM-dd", var27))) {
                                    ++var19;
                                }
                            }
                        } else {
                            var19 = var4.getExeTimes(var12, var5.parse(var23), var5.parse(var20), var18).size();
                        }
                    }

                    if (var6.containsKey(var11)) {
                        int var37 = (Integer)var6.get(var11);
                        var6.put(var11, var37 + var19);
                    } else {
                        var6.put(var11, var19 > 0 ? var19 : 0);
                    }
                }
            }
            List list4=jbInspectViewService.queryManualExecuteTask();//今天手动执行的任务
            HashMap map4=(HashMap)list4.get(0);
            int met_count=Integer.parseInt(map4.get("COUNT").toString());//手动执行任务数
            int all_count=var3+met_count;//计划执行任务数（不包括手动）+手动执行任务数=今天需要总共执行的任务数
            int sc=Integer.parseInt(successCount);//执行成功任务数
            int fc=Integer.parseInt(failCount);//执行失败任务数
            int ec=Integer.parseInt(executeCount);//执行中任务数
            int yzx_count=sc+fc+ec;//已执行任务数=执行成功+执行失败+执行中任务数
            int wzx_count=all_count-(sc+fc+ec);//未执行任务数=任务总数-（执行成功+执行失败+执行中任务数）
            HashMap map4_yzx=new HashMap();
            HashMap map4_wzx=new HashMap();
            map4_yzx.put("COUNT",yzx_count);
            map4_yzx.put("DESCRIBE","已执行任务数");
            map4_wzx.put("COUNT",wzx_count);
            map4_wzx.put("DESCRIBE","未执行任务数");
            resultList.add(map4_yzx);
            resultList.add(map4_wzx);

            List list5=jbInspectViewService.querySystemHealthCount();//查询今天的系统健康度
            HashMap map5=(HashMap)list5.get(0);
            resultList.add(map5);

            List list6=jbInspectViewService.queryInspectCoverRate();//查询今天的巡检覆盖率
            HashMap map6=(HashMap)list6.get(0);
            resultList.add(map6);

            resultMap.put("list",resultList);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * queryInspectSponsorWay 巡检发起方式
     *
     * @return TbSysDepartment
     * @throws Exception the exception
     * @author xubh
     * @date 2018/01/06
     */
    @ResponseBody
    @RequestMapping(value = "/queryInspectSponsorWay", method = RequestMethod.POST)
    public HashMap queryInspectSponsorWay() throws Exception {
        HashMap resultMap=new HashMap();
        try{
           List list=jbInspectViewService.queryAutoManualInspectTask();
            resultMap.put("list",list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * queryInspectTaskExecuteStatus 巡检任务执行情况
     *
     * @return TbSysDepartment
     * @throws Exception the exception
     * @author xubh
     * @date 2018/01/06
     */
    @ResponseBody
    @RequestMapping(value = "/queryInspectTaskExecuteStatus", method = RequestMethod.POST)
    public HashMap queryInspectTaskExecuteStatus() throws Exception {
        HashMap resultMap=new HashMap();
        List resultList=new ArrayList();
        try{
            List list3=jbInspectViewService.queryTaskExecuteStatus();//查询任务执行成功/失败/执行中任务数
            HashMap map3_success=new HashMap();
            HashMap map3_fail=new HashMap();
            HashMap map3_execute=new HashMap();
            String successCount="0";//执行成功任务数
            String failCount="0";//执行失败任务数
            String executeCount="0";//执行中任务数
            if(list3!=null&&list3.size()>0){
                for(int i=0;i<list3.size();i++){
                    HashMap map3=(HashMap)list3.get(i);
                    String status=map3.get("STATUS").toString();
                    if("5".equals(status)){
                        successCount=map3.get("COUNT").toString();
                    }else if("2".equals(status)){
                        failCount=map3.get("COUNT").toString();
                    }else {
                        executeCount=map3.get("COUNT").toString();
                    }
                }
            }
            map3_success.put("COUNT",successCount);
            map3_success.put("DESCRIBE","执行成功任务");
            map3_fail.put("COUNT",failCount);
            map3_fail.put("DESCRIBE","执行失败任务");
            map3_execute.put("COUNT",executeCount);
            map3_execute.put("DESCRIBE","执行中任务");

            List var8 =jbInspectViewService.queryPlanExecuteCount();//计划执行数
            int var3 = 0;//计划执行任务数
            QuartzUtil var4 = new QuartzUtil();
            SimpleDateFormat var5 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            HashMap var6 = new HashMap();
            Date var7 = new Date();
            int var19;
            if (var8 != null && var8.size() > 0) {
                for(Iterator var9 = var8.iterator(); var9.hasNext(); var3 += var19 > 0 ? var19 : 0) {
                    Map var10 = (Map)var9.next();
                    String var11 = var10.get("TASK_ID").toString();
                    String var12 = var10.get("CRONTAB").toString();
                    Date var13 = (Date)var10.get("CREATE_TIME");
                    Object var14 = var10.get("PREV_FIRE_TIME");
                    String var15 = var10.get("ENABLE").toString();
                    Date var16 = (Date)var10.get("UPDATE_TIME");
                    Long var17 = -1L;
                    if (var14 != null) {
                        var17 = Long.parseLong(var10.get("PREV_FIRE_TIME").toString());
                    }

                    new Date();
                    Date var18;
                    if (var17 == -1L) {
                        var18 = var13;
                    } else {
                        var18 = new Date(var17);
                    }

                    var19 = 0;
                    String var20 = "";
                    if (DateUtil.getDate(var7).equals(DateUtil.getDate(var13))) {
                        String var21 = DateUtil.getDateTime("yyyy-MM-dd", var13);
                        var20 = var21 + " 23:59:59";
                        if ("2".equals(var15)) {
                            if (DateUtil2.after(var5.parse(var20), var16)) {
                                var20 = var5.format(var16);
                            } else {
                                var20 = var21 + " 23:59:59";
                            }
                        }

                        var19 = var4.getExeTimes(var12, var13, var5.parse(var20), var18).size();
                    } else if (DateUtil2.after(var7, var13)) {
                        String var22 = DateUtil.getDateTime("yyyy-MM-dd", var7);
                        String var23 = var22 + " 00:00:00";
                        var20 = var22 + " 23:59:59";
                        if ("2".equals(var15)) {
                            if (DateUtil2.after(var5.parse(var20), var16)) {
                                var20 = var5.format(var16);
                            } else {
                                var20 = var22 + " 23:59:59";
                            }
                        }

                        String[] var24 = var12.split(" ");
                        if (var24[3].contains("/")) {
                            List var25 = var4.getExeTimes(var12, var5.parse(var23), var5.parse(var20), var18);
                            Iterator var26 = var25.iterator();

                            while(var26.hasNext()) {
                                Date var27 = (Date)var26.next();
                                if (var22.equals(DateUtil.getDateTime("yyyy-MM-dd", var27))) {
                                    ++var19;
                                }
                            }
                        } else {
                            var19 = var4.getExeTimes(var12, var5.parse(var23), var5.parse(var20), var18).size();
                        }
                    }

                    if (var6.containsKey(var11)) {
                        int var37 = (Integer)var6.get(var11);
                        var6.put(var11, var37 + var19);
                    } else {
                        var6.put(var11, var19 > 0 ? var19 : 0);
                    }
                }
            }
            List list4=jbInspectViewService.queryManualExecuteTask();//今天手动执行的任务
            HashMap map4=(HashMap)list4.get(0);
            int met_count=Integer.parseInt(map4.get("COUNT").toString());//手动执行任务数
            int all_count=var3+met_count;//计划执行任务数（不包括手动）+手动执行任务数=今天需要总共执行的任务数
            int sc=Integer.parseInt(successCount);//执行成功任务数
            int fc=Integer.parseInt(failCount);//执行失败任务数
            int ec=Integer.parseInt(executeCount);//执行中任务数
            int yzx_count=sc+fc+ec;//已执行任务数=执行成功+执行失败+执行中任务数
            int wzx_count=all_count-(sc+fc+ec);//未执行任务数=任务总数-（执行成功+执行失败+执行中任务数）
            HashMap map4_yzx=new HashMap();
            HashMap map4_wzx=new HashMap();
            map4_yzx.put("COUNT",yzx_count);
            map4_yzx.put("DESCRIBE","已执行任务");
            map4_wzx.put("COUNT",wzx_count);
            map4_wzx.put("DESCRIBE","未执行任务");
            resultList.add(map4_wzx);
            resultList.add(map4_yzx);
            resultList.add(map3_execute);
            resultList.add(map3_success);
            resultList.add(map3_fail);
            resultMap.put("list",resultList);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * queryInspectTaskTimeDis 巡检任务时间分布
     *
     * @return TbSysDepartment
     * @throws Exception the exception
     * @author xubh
     * @date 2018/01/06
     */
    @ResponseBody
    @RequestMapping(value = "/queryInspectTaskTimeDis", method = RequestMethod.POST)
    public HashMap queryInspectTaskTimeDis() throws Exception {
        HashMap resultMap=new HashMap();
        HashMap hourMap=new HashMap();
        List resultList=new ArrayList();
        try{
            List list=jbInspectViewService.queryInspectTaskTimeDis();//查询当天巡检任务分布时间（不一定每个小时都有任务）
            if(list!=null&&list.size()>0){
                for(int i=0;i<list.size();i++){
                    HashMap map=(HashMap)list.get(i);
                    String count=map.get("COUNT").toString();
                    String hour=map.get("HOUR").toString();
                    hourMap.put(hour.substring(11),count);//封装查询到的每个小时的巡检任务数量
                }
            }
            for(int j=0;j<24;j++){
                String key="";
                String hour=""+j;
                if(j<10){
                    key="0"+j;
                }else{
                    key=""+j;
                }
                String task_count=hourMap.get(key)==null?"0":hourMap.get(key).toString();
                HashMap tcMap=new HashMap();
                tcMap.put("HOUR",hour);
                tcMap.put("COUNT",task_count);//封装24小时每个小时的任务数量（没有的为0）
                resultList.add(tcMap);
            }
            resultMap.put("list",resultList);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * querySystemThirtyDayHealth 系统30天健康度
     *
     * @return TbSysDepartment
     * @throws Exception the exception
     * @author xubh
     * @date 2018/01/06
     */
    @ResponseBody
    @RequestMapping(value = "/querySystemThirtyDayHealth", method = RequestMethod.POST)
    public HashMap querySystemThirtyDayHealth() throws Exception {
        HashMap resultMap=new HashMap();
        try{
            List list=jbInspectViewService.querySystemThirtyDayHealth();
            resultMap.put("list",list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * queryThirtyDayCoverTrend 30天覆盖趋势
     *
     * @return TbSysDepartment
     * @throws Exception the exception
     * @author xubh
     * @date 2018/01/06
     */
    @ResponseBody
    @RequestMapping(value = "/queryThirtyDayCoverTrend", method = RequestMethod.POST)
    public HashMap queryThirtyDayCoverTrend() throws Exception {
        HashMap resultMap=new HashMap();
        try{
            List list=jbInspectViewService.queryThirtyDayCoverTrend();
            resultMap.put("list",list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * queryTaskGroupHealth 任务分组健康度
     *
     * @return TbSysDepartment
     * @throws Exception the exception
     * @author xubh
     * @date 2018/01/06
     */
    @ResponseBody
    @RequestMapping(value = "/queryTaskGroupHealth", method = RequestMethod.POST)
    public HashMap queryTaskGroupHealth() throws Exception {
        long a = System.currentTimeMillis();
        System.out.println(a);
        HashMap resultMap=new HashMap();
        List resultList=new ArrayList();
        try{
            List glist=jbInspectViewService.queryTaskGroup();//查询所有根节点任务组
            if(glist!=null&&glist.size()>0){
                for(int i=0;i<glist.size();i++){
                    HashMap gmap=(HashMap)glist.get(i);
                    List hlist=jbInspectViewService.queryTaskGroupHealth(gmap);//查询每个任务组当天的健康度
                    List list=jbInspectViewService.queryTaskGroupThirtyDayHealth(gmap);//查询每个任务组30天的健康度
                    HashMap map=(HashMap)hlist.get(0);
                    map.put("THIRTY_DAY",list);
                    resultList.add(map);
                }
            }
            resultMap.put("list",resultList);
        }catch (Exception e){
            e.printStackTrace();
        }
        long b = System.currentTimeMillis();
        System.out.println(b);
        System.out.println("执行时间为：==="+(b-a)/1000+"秒");
        return resultMap;
    }

    public static void main(String args[]) throws Exception {
        Calendar cal = Calendar.getInstance ();
        Calendar cal1 = Calendar.getInstance ();
        List dayList=new ArrayList();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for(int i=1;i<=30;i++){
            if(i==1){
                String ond_day=dateFormat.format(cal.getTime());
                dayList.add(ond_day.substring(8));
                System.out.println(ond_day.substring(8));
            }else{
                cal1.set(Calendar.DATE, cal1.get(Calendar.DATE) - 1);
                String every_day=dateFormat.format(cal1.getTime());
                dayList.add(every_day.substring(8));
                System.out.println(every_day.substring(8));
            }
        }
        //System.out.println("开始时间："+starttime+"========"+"结束时间："+endtime);
    }
}
